VERSION 4.00
Begin VB.Form frmQPF 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Performance Counter Class Demo"
   ClientHeight    =   1905
   ClientLeft      =   1245
   ClientTop       =   1545
   ClientWidth     =   5535
   ClipControls    =   0   'False
   Height          =   2310
   Left            =   1185
   LinkTopic       =   "Form1"
   LockControls    =   -1  'True
   MaxButton       =   0   'False
   ScaleHeight     =   127
   ScaleMode       =   3  'Pixel
   ScaleWidth      =   369
   Top             =   1200
   Width           =   5655
   Begin VB.CommandButton Command2 
      Caption         =   "VB &Timer Start"
      Height          =   420
      Left            =   4185
      TabIndex        =   3
      Top             =   1350
      Width           =   1230
   End
   Begin VB.TextBox Text1 
      Alignment       =   1  'Right Justify
      Height          =   285
      Left            =   4185
      MaxLength       =   8
      TabIndex        =   1
      Text            =   "1000000"
      Top             =   315
      Width           =   1185
   End
   Begin VB.CommandButton Command1 
      Caption         =   "&Loop Test"
      Height          =   420
      Left            =   4185
      TabIndex        =   2
      Top             =   720
      Width           =   1230
   End
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   1000
      Left            =   1485
      Top             =   1035
   End
   Begin VB.Label Label6 
      BeginProperty Font 
         name            =   "MS Sans Serif"
         charset         =   0
         weight          =   700
         size            =   8.25
         underline       =   0   'False
         italic          =   0   'False
         strikethrough   =   0   'False
      EndProperty
      Height          =   195
      Index           =   1
      Left            =   1260
      TabIndex        =   15
      Top             =   1575
      UseMnemonic     =   0   'False
      Width           =   2775
   End
   Begin VB.Label Label5 
      Height          =   195
      Index           =   1
      Left            =   1350
      TabIndex        =   14
      Top             =   1350
      UseMnemonic     =   0   'False
      Width           =   2700
   End
   Begin VB.Label Label6 
      AutoSize        =   -1  'True
      Caption         =   "VB Timer Actual:"
      Height          =   195
      Index           =   0
      Left            =   45
      TabIndex        =   13
      Top             =   1575
      UseMnemonic     =   0   'False
      Width           =   1185
   End
   Begin VB.Label Label5 
      Caption         =   "VB Timer Interval:"
      Height          =   195
      Index           =   0
      Left            =   45
      TabIndex        =   12
      Top             =   1350
      UseMnemonic     =   0   'False
      Width           =   1260
   End
   Begin VB.Label Label2 
      Height          =   195
      Index           =   3
      Left            =   855
      TabIndex        =   11
      Top             =   495
      UseMnemonic     =   0   'False
      Width           =   3225
   End
   Begin VB.Label Label2 
      Caption         =   "Resolution:"
      Height          =   195
      Index           =   2
      Left            =   45
      TabIndex        =   10
      Top             =   495
      UseMnemonic     =   0   'False
      Width           =   795
   End
   Begin VB.Label Label4 
      AutoSize        =   -1  'True
      Caption         =   "Loop &Iterations:"
      Height          =   195
      Left            =   4185
      TabIndex        =   0
      Top             =   105
      Width           =   1095
   End
   Begin VB.Label Label3 
      BeginProperty Font 
         name            =   "MS Sans Serif"
         charset         =   0
         weight          =   700
         size            =   8.25
         underline       =   0   'False
         italic          =   0   'False
         strikethrough   =   0   'False
      EndProperty
      Height          =   195
      Index           =   1
      Left            =   1395
      TabIndex        =   9
      Top             =   900
      UseMnemonic     =   0   'False
      Width           =   2625
   End
   Begin VB.Label Label3 
      AutoSize        =   -1  'True
      Caption         =   "Loop test elapsed:"
      Height          =   195
      Index           =   0
      Left            =   45
      TabIndex        =   8
      Top             =   900
      UseMnemonic     =   0   'False
      Width           =   1305
   End
   Begin VB.Label Label2 
      Height          =   195
      Index           =   1
      Left            =   855
      TabIndex        =   7
      Top             =   270
      UseMnemonic     =   0   'False
      Width           =   3225
   End
   Begin VB.Label Label2 
      Caption         =   "Frequency:"
      Height          =   195
      Index           =   0
      Left            =   45
      TabIndex        =   6
      Top             =   270
      UseMnemonic     =   0   'False
      Width           =   795
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "NO"
      Height          =   195
      Index           =   1
      Left            =   2295
      TabIndex        =   5
      Top             =   45
      UseMnemonic     =   0   'False
      Width           =   240
   End
   Begin VB.Label Label1 
      Caption         =   "Performance Counter Available:"
      Height          =   195
      Index           =   0
      Left            =   45
      TabIndex        =   4
      Top             =   45
      UseMnemonic     =   0   'False
      Width           =   2235
   End
End
Attribute VB_Name = "frmQPF"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
'   Sample project utilizing cPerfCounter class
'=================================

Option Explicit

Private Const MAXLOOPS = 10000000 'upper limit for test loop iterations
Dim PC1 As New cPerfCounter 'initialize class reference

Private Function QualifyLimit() As Long
    'Make sure the test loop limit entered by user is numeric, >0, and <MAXLOOPS
    Dim lVal As Long
    
    If Not IsNumeric(Text1.Text) Then
        lVal = 0
    Else
        lVal = CLng(Text1.Text)
        If lVal > MAXLOOPS Then lVal = MAXLOOPS
        If lVal < 0 Then lVal = 0
    End If
    QualifyLimit = lVal
    Text1.Text = CStr(lVal) 'reflect any changes
End Function

Private Sub Command1_Click()
    'Loop Test button
    Dim dElapsed As Double
    Dim lCount As Long
    
    Me.Enabled = False 'disable to prevent re-entry
    Screen.MousePointer = vbHourglass
    Command1.Caption = "Running"
    
    DoEvents 'allow any pending events to execute before starting
    PC1.Reset 'mark the start time
    For lCount = 0& To QualifyLimit
        'just counting...
    Next lCount
    dElapsed = PC1.Elapsed 'get elapsed time
    Label3(1).Caption = CStr(dElapsed) & " sec"
    Command1.Caption = "&Loop Test"
    DoEvents  'Dump any mouse or key events that may have ocurred
              'while the loop was running before we re-enable form.
              '(Prevents a doubleclick from running it twice.)
    
    Me.Enabled = True
    Screen.MousePointer = vbDefault
End Sub

Private Sub Command2_Click()
    'VB Timer Start/Stop
    Timer1.Enabled = Not Timer1.Enabled
    If Timer1.Enabled Then
        Command2.Caption = "VB &Timer Stop"
        PC1.Reset
    Else
        Command2.Caption = "VB &Timer Start"
    End If
End Sub

Private Sub Form_Load()
    Label1(1).Caption = IIf(PC1.Available, "Yes", "No")
    Command1.Enabled = PC1.Available
    
    'show frequency...
    Label2(1).Caption = Format$(PC1.Frequency, "#,###,###,###") & "/sec"
    
    'show resolution...
    Label2(3).Caption = CStr(1# / PC1.Frequency) & " sec"
    
    Timer1.Interval = 1000
    Label5(1).Caption = Timer1.Interval
    Me.Move (Screen.Width - Width) / 2, (Screen.Height - Height) / 3
    Me.Show
    DoEvents
    If PC1.Available = False Then
        MsgBox "This system does not have a Performance Counter.", vbOKOnly + vbCritical + vbDefaultButton1 + vbApplicationModal, "Performance Counter Not Detected"
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Timer1.Enabled = False
    Set PC1 = Nothing 'terminate class instance
    Set frmQPF = Nothing
End Sub

Private Sub Text1_GotFocus()
    'select all...
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1.Text)
End Sub

Private Sub Timer1_Timer()
    Dim dActual As Double, lCount As Long
    
    dActual = PC1.Elapsed
    PC1.Reset
    
    'show actual time elapsed between timer events
    Label6(1).Caption = CStr(dActual) & " sec"
    
    'The rate at which a VB / Win32 timer actually fires is infuenced by both
    'external events and the time required to execute code in the Timer event...
    For lCount = 0& To QualifyLimit
        'count...
    Next
End Sub
